|
|
|
|
|
|
|
perform multiple commands by calling a procedure. The following is the syntax of the single-line If statement: |
|
|
|
|
|
|
|
|
If condition Then command |
|
|
|
|
|
|
|
|
The argument condition represents any type of logical condition, which can be any of the following: |
|
|
|
|
|
|
|
|
Comparison of a variable to a literal, another variable, or a function |
|
|
|
|
|
|
|
|
A variable or database field that contains a True or False value |
|
|
|
|
|
|
|
|
Any function or expression that returns a True or False value |
|
|
|
|
|
|
|
|
The argument command represents the task to be performed if the condition is True. This task can be any valid Visual Basic statement, including a procedure call. The following code shows how an If statement is used to print an employee's name only if his or her salary is greater than a predefined maximum value: |
|
|
|
|
|
|
|
|
If cSalary > cMaxSalary Then Printer.Print sEmpName |
|
|
|
|
|
|
|
|
Multiple Commands for a Condition |
|
|
|
|
|
|
|
|
If you need to execute more than one command in response to a condition, you can use the multiple-line form of the If statement. It is also known as a block If statement. This construct bounds a range of statements between the If statement and an End If statement. If the condition in the If statement is True, all the commands between the If and End If statements are executed. If the condition is False, the program skips to the first line after the End If statement. The following example shows how a block If statement is used in processing a market exhibitor's payments. If the exhibitor has a deposit on file, the deposit amount is moved to the total amount paid, and a procedure that processes a reservation is called. |
|
|
|
|
|
|
|
|
If cDepositAmt > 0 Then
cTotalPaid = cTotalPaid + cDepositAmt
cDepositAmt = 0
Call UpdateReservation
End If |
|
|
|
|
|
|
|
|
Working with the False Condition |
|
|
|
|
|
|
|
|
Of course, if a condition can be True, it can also be False; and sometimes you might want code to execute only on a False condition. Other times, you might want to take one action if a condition is True and another action if the condition is False. The following sections look at handling the False side of a condition. |
|
|
|
|
|
|
|
|
One way to execute a statement, or group of statements, for a False condition is to use the Not operator. The Not operator inverts the actual condition that follows it. If the condition is True, the Not operator makes the overall expression False, and vice versa. The following code uses the Not operator to invert the value of the Boolean variable bTaxable, which reports an exhibitor's sales and use tax status. bTaxable is True if the exhibitor is to pay sales and use taxes, and False if he or she is not. The code tests for the condition Not bTaxable; if this condition evaluates to True, the exhibitor is not taxable. |
|
|
|
|
|